<?php
namespace User\Controller;
use Think\Controller;
use Think\Page;
use User\Model\CardModel;
use User\Model\CardRegisterModel;
use User\Model\FactoryModel;

class CardController extends UserController
{
    public function __construct()
    {
        parent::__construct();
        if (!headers_sent() && // 如果页面头部信息还没有输出
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }
    }

    public function index()
    {
        $cond['status'] = array("NEQ", CardModel::STATUS_DELETE);
        $cond['token'] = $this->wxuser['token'];

        $count = M("card")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("card")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('card_id desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function add()
    {
        $params = I("post.", "", "");
        if (empty($params)) {
            $type = 'add';
            $this->assign('type', $type);
            return $this->display('update');
        }
        if (empty($params['name'])) {
            return $this->my_error("请填写名称");
        }
        $data['name'] = $params['name'];
        $data['token'] = $this->wxuser['token'];
        $data['start_date'] = $params['start_date'];
        $data['end_date'] = $params['end_date'];
        $data['status'] = CardModel::STATUS_PENDING;
        M("card")->add($data);
        return $this->my_success("成功");
    }

    public function update()
    {
        $type = I("get.type", "", "");
        $cardId = I("get.cardId", "", "");
        $params = I("post.", "", "");
        if ($type == 'update' && empty($params)) {
            $card = M("card")->where(['card_id' => $cardId])->find();
            $this->assign("card", $card);
            return $this->display();
        }
        $cardId = I("post.cardId", "", "");
        if (empty($cardId)) {
            return $this->my_error("选择的卡片信息有误");
        }
        $data['name'] = $params['name'];
        $data['start_date'] = $params['start_date'];
        $data['end_date'] = $params['end_date'];
        M("card")->where(['card_id' => $cardId])->save($data);
        return $this->my_success("成功");
    }

    public function delete()
    {
        $cardId = I("get.cardId", "", "");
        $data['status'] = CardModel::STATUS_DELETE;
        if (!empty($cardId)) {
            M("card")->where(['card_id' => $cardId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function pass()
    {
        $cardId = I("get.cardId", "", "");
        $data['status'] = CardModel::STATUS_ACTIVE;
        if (!empty($cardId)) {
            M("card")->where(['card_id' => $cardId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function unpass()
    {
        $cardId = I("get.cardId", "", "");
        $data['status'] = CardModel::STATUS_PENDING;
        if (!empty($cardId)) {
            M("card")->where(['card_id' => $cardId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function register()
    {
        $params = I("get.", "", "");
        if (empty($params['cardId'])) {
            return $this->redirect("/User/Card/index");
        }
        $cond['card_id'] = $params['cardId'];
        $cond['status'] = array("NEQ", CardRegisterModel::STATUS_DELETE);
        if (!empty($params['status'])) {
            $cond['status'] = $params['status'];
        }
        if (!empty($params['mobile'])) {
            $cond['mobile'] = $params['mobile'];
        }
        if (!empty($params['name'])) {
            $cond['name'] = $params['name'];
        }
        $count = M("card_register")->where($cond)->count();

        $page = new Page($count, 10);
        $show = $page->show();
        $this->assign("page", $show);

        $list = M("card_register")->where($cond)->limit($page->firstRow . ',' . $page->listRows)->order('reg_id desc')->select();
        $this->assign("list", $list);
        return $this->display();
    }


    public function registerUpdate()
    {
        $regId = I("get.regId", "", "");
        $cardRegister = M("card_register")->where(['reg_id' => $regId])->find();
        $this->assign("cardRegister", $cardRegister);
        return $this->display();
    }

    public function registerDelete()
    {
        $regId = I("get.regId", "", "");
        $data['status'] = CardRegisterModel::STATUS_DELETE;
        if (!empty($regId)) {
            M("card_register")->where(['reg_id' => $regId])->save($data);
        }
        return $this->my_success("成功");
    }

    public function download()
    {
        set_time_limit(0);
        $params = I("get.", "", "");
        if (empty($params['cardId'])) {
            return $this->redirect("/User/Card/index");
        }
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $objActSheet->getColumnDimension('A')->setWidth(30);
        $objActSheet->getColumnDimension('B')->setWidth(30);
        $objActSheet->getColumnDimension('C')->setWidth(30);
        $objActSheet->getColumnDimension('D')->setWidth(30);
        $objActSheet->getColumnDimension('E')->setWidth(30);
        $objActSheet->setCellValue('A1', "名称");
        $objActSheet->setCellValue('B1', "电话");
        $objActSheet->setCellValue('C1', "图片");
        $objActSheet->setCellValue('D1', "内容");
        $objActSheet->setCellValue('E1', "添加时间");
        $cond['card_id'] = $params['cardId'];
        $cond['status'] = array("NEQ", CardRegisterModel::STATUS_DELETE);
        if (!empty($params['status'])) {
            $cond['status'] = $params['status'];
        }
        if (!empty($params['mobile'])) {
            $cond['mobile'] = $params['mobile'];
        }
        if (!empty($params['email'])) {
            $cond['name'] = $params['name'];
        }
        $line = 2;
        $count = M("card_register")->where($cond)->count();
        $number = $count / 100;
        for ($i = 0; $i <= $number; $i++) {
            $list = M("card_register")->where($cond)->limit($i * 100, 100)->order('reg_id desc')->select();
            foreach ($list as $register) {
                $objActSheet->setCellValue('A' . $line, $register['name']);
                $objActSheet->setCellValue('B' . $line, $register['mobile']);
                $path = substr($register['picurl'], 1);
                $path_arr = explode(".", $path);
                $path = $path_arr[0] . "thumb." . $path_arr[1];
                $objDrawing = new \PHPExcel_Worksheet_Drawing();
                $objDrawing->setPath($path);
                $objDrawing->setHeight(120);
                $objDrawing->setWidth(120);
                $objDrawing->setCoordinates('C' . $line);
                $objDrawing->setOffsetX(10);
                $objDrawing->setWorksheet($objActSheet);
                $objActSheet->setCellValue('D' . $line, $register['content']);
                $objActSheet->setCellValue('E' . $line, $register['added_date']);
                $objActSheet->getRowDimension($line)->setRowHeight(100);
                $line++;
            }
        }
        $fileName = '卡片' . date('Ymd') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function downPic()
    {
        $filepath = I('get.path', '', '');
        $filepath = substr($filepath, 1);
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($filepath));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filepath));
        readfile($filepath);
    }

    public function share()
    {
        $params = I('post.', '', '');
        if (empty($params)) {
            $id = I('get.id', '', '');
            if (empty($id)) {
                return $this->redirect('/User/Card/index');
            }
            $card = M("card")->where(['card_id' => $id])->find();
            if (empty($card)) {
                return $this->redirect('/User/Card/index');
            }
            $this->assign('card', $card);
            return $this->display();
        } else {
            if (!empty($params['id'])) {
                $data['share_title'] = $params['share_title'];
                $data['share_desc'] = $params['share_desc'];

                $img = $params['share_pic'];
                if (!empty($img)) {
                    $ajaxImg = FactoryModel::ajaxImgUpload($img, "card");
                    if ($ajaxImg['code'] != 1) {
                        return $this->my_error('图片上传失败');
                    }
                    $data['share_pic'] = $ajaxImg['msg'];
                }
                M("card")->where(['card_id' => $params['id']])->save($data);
            }
            return $this->my_success("成功");
        }
    }
}

?>
